package com.manage.controller;

import com.beans.HouseInfo;
import com.beans.Phone;
import com.beans.page.PageList;
import com.manage.util.UserUtils;
import com.services.HouseInfoService;
import com.services.PhoneService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * @author jiangwei
 * @since 18/1/19
 */
@Controller
@RequestMapping("phone")
public class PhoneController extends BaseController {
    @Autowired
    public PhoneService phoneService;
    @Autowired
    public HouseInfoService houseInfoService;

    /**
     * 查询列表
     *
     * @param phone
     * @return
     */
    @RequiresPermissions("phone:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(Phone phone, Long id, HttpServletRequest request) {
        phone.setHouseId(id);
        request.setAttribute("phone", phone);

        Phone p = new Phone();
        p.setHouseId(id);
        p.setCreateBy(UserUtils.getUser().getUserId());
        PageList list = phoneService.queryPage(new PageList<Phone>(), p);
        if (list.getiTotalDisplayRecords() > 0) {
            HouseInfo info = houseInfoService.queryById(id);
            request.setAttribute("tel", info.getOwnerPhone());
        }
        return "phone/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param phone
     * @return
     */
    @RequiresPermissions("phone:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, Phone phone) {
        return phoneService.queryPage(pageList, phone);
    }

    @RequiresPermissions("phone:apply")
    @RequestMapping("save")
    @ResponseBody
    public boolean add(Long id, String remark) {
        boolean returnValue = false;

        Phone phone = new Phone();

        phone.setCreateBy(UserUtils.getUser().getUserId());
        phone.setUpdateBy(UserUtils.getUser().getUserId());
        phone.setHouseId(id);
        phone.setName(UserUtils.getUser().getUsername());
        phone.setRemark(remark);

        phone.setCreateDate(new Timestamp(System.currentTimeMillis()));
        phone.setUpdateDate(new Timestamp(System.currentTimeMillis()));

        int rsout = phoneService.add(phone);

        if (rsout > 0) {
            returnValue = true;
        }

        return returnValue;
    }
}
